package com.yx.frame.builder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class AddFileDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text fileNameInput;
	private Button button;
	private Button button_1;
	private Label lbljava;
	LeftControlTree leftControlTree;
	private Button button_2;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public AddFileDialog(Shell parent, int style,LeftControlTree leftControlTree) {
		super(parent, style);
		this.leftControlTree=leftControlTree;
		setText("添加新文件");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.BORDER | SWT.TITLE
				| SWT.PRIMARY_MODAL);
		shell.setSize(450, 157);
		shell.setText(getText());

		Label label = new Label(shell, SWT.NONE);
		label.setBounds(10, 12, 48, 17);
		label.setText("文件名：");

		fileNameInput = new Text(shell, SWT.BORDER);
		fileNameInput.setEditable(false);
		fileNameInput.setBounds(72, 10, 339, 23);

		Label lblxml = new Label(shell, SWT.NONE);
		lblxml.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		lblxml.setForeground(SWTResourceManager.getColor(255, 0, 0));
		lblxml.setText("需要填写基于工作空间的相对路径，文件类型为rsb");
		lblxml.setBounds(10, 45, 425, 17);

		button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String fileName = fileNameInput.getText().trim();
				if (fileName.equals("")) {
					return;
				}
				if(!fileName.endsWith(".rsb")){
					MessageBox messageBox = new MessageBox(shell, SWT.OK);
					messageBox.setMessage("文件扩展名必须为“.rsb”(采用小写)!");
					if (messageBox.open() == SWT.OK) {
						return;
					} else {
						return;
					}
				}
				String pathStr = "";
				if (fileName.lastIndexOf("\\") > 0) {
					pathStr = fileName.substring(0, fileName.lastIndexOf("\\"));
				}
				File path = new File(pathStr);
				if (!path.exists()) {
					path.mkdirs();
				}
				File file = new File(fileName);
				if (file.exists()) {
					MessageBox messageBox = new MessageBox(shell, SWT.OK);
					messageBox.setMessage("该路径中存在同名文件，请选择其他文件名!");
					if (messageBox.open() == SWT.OK) {
						return;
					} else {
						return;
					}
				}
				try {
					OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
					//FileWriter writer = new FileWriter(file);
					writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
					writer.write("<ServiceBuilder>\r\n");
					writer.write("</ServiceBuilder>\r\n");
					writer.flush();
					writer.close();
					leftControlTree.refrashTree();
					shell.close();
					shell.dispose();
				} catch (Exception ex) {
					MessageBox messageBox = new MessageBox(shell, SWT.OK);
					messageBox.setMessage("创建文件出错：" + ex.getMessage());
					if (messageBox.open() == SWT.OK) {
						return;
					} else {
						return;
					}
				}

			}
		});
		button.setBounds(257, 92, 80, 27);
		button.setText("确定");

		button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
				shell.dispose();
			}
		});
		button_1.setText("取消");
		button_1.setBounds(354, 92, 80, 27);

		lbljava = new Label(shell, SWT.NONE);
		lbljava.setText("文件命名方式按照JAVA类命名习惯，用于生成JAVA对象");
		lbljava.setForeground(SWTResourceManager.getColor(255, 0, 0));
		lbljava.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		lbljava.setBounds(9, 68, 425, 17);
		
		button_2 = new Button(shell, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog filedlg=new FileDialog(shell,SWT.OPEN);
				//设置文件对话框的标题
				filedlg.setText("文件选择");
				//设置初始路径
				filedlg.setFilterPath(leftControlTree.ROOT_PATH);
				//打开文件对话框，返回选中文件的绝对路径
				String selected=filedlg.open();
				if(selected==null)selected="";
				fileNameInput.setText(selected);
			}
		});
		button_2.setText("...");
		button_2.setBounds(414, 8, 28, 27);
	}

}
